//
//  MessageTableViewCell.swift
//  RoundCatPlay2
//
//  Created by 姜澎 on 2019/10/26.
//  Copyright © 2019 jiangpeng. All rights reserved.
//

import UIKit

class MessageTableViewCell: UITableViewCell {
    
    var messageModel: MessageModel? {
        didSet {
            self.leftImage.image = UIImage(named: messageModel!.titleImageName)
            self.titleLable.text = messageModel?.title
            self.detailLabel.text = messageModel?.contentTitle
            self.dateLable.text = messageModel?.date
            self.MessageNumberL.text = messageModel?.messageNum
        }
    }
    
    private lazy var leftImage: UIImageView = {
        let i = UIImageView()
        i.contentMode = .scaleAspectFit
        i.layer.cornerRadius = 5
        return i
    }()
    
    private lazy var titleLable: UILabel = {
        let t = UILabel()
        t.font = UIFont.boldSystemFont(ofSize: 17)
        t.sizeToFit()
        return t
    }()
    
    private lazy var detailLabel: UILabel = {
        let d = UILabel()
        d.sizeToFit()
        d.textColor = UIColor.darkGray
        d.font = UIFont.systemFont(ofSize: 14)
        return d
    }()
    
    private lazy var dateLable: UILabel = {
        let d = UILabel()
        d.font = UIFont.systemFont(ofSize: 14)
        d.textColor = UIColor.darkGray
        d.sizeToFit()
        return d
    }()
    
    
    public lazy var MessageNumberL: UILabel = {
        let m = UILabel()
        
        m.layer.cornerRadius = 8
        m.backgroundColor = .red
        m.textColor = .white
        m.textAlignment = .center
        m.clipsToBounds = true
        m.font = UIFont.systemFont(ofSize: 12)
        return m
    }()
    
    
    private lazy var bottomLine: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(red: 234/255, green: 234/255, blue: 234/255, alpha: 1)
        return v
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        
    }
    
}

extension MessageTableViewCell {
    
    func setUI(){
        
        addSubview(self.leftImage)
        addSubview(self.titleLable)
        addSubview(self.detailLabel)
        addSubview(self.dateLable)
        addSubview(self.MessageNumberL)
        addSubview(self.bottomLine)
        
        setleftImageFrame()
        settitleLableFrame()
        setDetailLabelFrame()
        setDateLableFrame()
        setMessageLabelFrame()
        setBottomLine()
    }
    
    func setleftImageFrame() {
        self.leftImage.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.equalTo(10)
            make.bottom.equalTo(-10)
            make.width.equalTo(70)
        }
    }
    
    func settitleLableFrame() {
        self.titleLable.snp.makeConstraints { (make) in
            make.top.equalTo(self.leftImage.snp_top).offset(2)
            make.left.equalTo(self.leftImage.snp_right).offset(10)
        }
    }
    
    func setDetailLabelFrame() {
        self.detailLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLable.snp_bottom).offset(8)
            make.left.equalTo(self.titleLable.snp_left)
            make.right.equalTo(-50)
        }
    }
    
    func setDateLableFrame() {
        self.dateLable.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLable.snp_top)
            make.right.equalTo(self.MessageNumberL.snp_right)
        }
    }
    
    func setMessageLabelFrame()  {
        self.MessageNumberL.snp.makeConstraints { (make) in
            make.top.equalTo(self.detailLabel.snp_top)
            make.right.equalTo(-18)
            make.width.equalTo(16)
            make.height.equalTo(16)
        }
    }
    
    func setBottomLine() {
        self.bottomLine.snp.makeConstraints { (make) in
            make.left.equalTo(23)
            make.right.equalTo(-18)
            make.bottom.equalTo(0)
            make.height.equalTo(1)
        }
    }
}
